package com.nlp.visualization.service.impl;

import com.nlp.visualization.core.discourse.IBayesService;
import com.nlp.visualization.core.discourse.IDiscourseService;
import com.nlp.visualization.core.discourse.IWordsCloudService;
import com.nlp.visualization.pojo.NLP.discourse.WordFrequencyTable;
import com.nlp.visualization.pojo.NLP.discourse.WordsCloudOption;
import com.nlp.visualization.service.IDataDiscourseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.util.*;

@Service
public class DataDiscourseServiceImpl implements IDataDiscourseService {


    Logger logger = LoggerFactory.getLogger(DataDiscourseServiceImpl.class);

    @Autowired
    IWordsCloudService wordsCloudService;

    @Autowired
    IDiscourseService discourseService;

    @Autowired
    IBayesService bayesService;

    /**
     * 词云接口，返回词云的一张图片
     *
     * @param texts
     * @param option
     * @return
     */
    @Override
    public File wordClould(List texts, WordsCloudOption option) throws IOException {

        logger.info("生成词云中...");
        File file = null;
        switch (option.getBgType()) {
            case CIRCLE:
                file = wordsCloudService.CicleCloud(texts, option);
                break;
            case RECTANGLE:
                file = wordsCloudService.RectangleCloud(texts, option);
                break;
            default:
                file = wordsCloudService.imageCloud(texts, option);
                break;
        }
        logger.info("生成词云成功,保存至" + file.getAbsolutePath());
        return file;
    }

    /**
     * 统计词频接口，返回一张词频表
     *
     * @param texts
     * @return
     * @throws Exception
     */
    @Override
    public WordFrequencyTable wordFrequencyCount(List texts) throws Exception {
        logger.info("统计词频中...");
        String text = null;
        Iterator<String> iterator = texts.iterator();
        while (iterator.hasNext()) {
            String str = iterator.next();
            text += str;
        }
        logger.info("统计词频结束...");
        return discourseService.countWordFrequency(text);
    }


    /**
     * 获取命名实体接口，返回命名实体的集合
     *
     * @param text
     * @return
     * @throws Exception
     */
    @Override
    public List wordNamedEntity(String text) throws Exception {
        logger.info("分析命名实体中...");
        List list = discourseService.getNamedEntity(text);
        Collections.sort(list, new Comparator<Map>() {
            @Override
            public int compare(Map o1, Map o2) {
                return ((String) o1.get("pos")).compareTo(((String) o2.get("pos")));
            }
        });
        logger.info("分析命名实体结束...");
        return list;

    }

    @Override
    public List getKeywords(String text, int count) throws Exception {
        logger.info("正在提取关键字...");
        List list = discourseService.getKeywords(text, count);
        logger.info("提取关键字完成");
        return list;

    }

    @Override
    public List mapRemark(String text) throws Exception {
        logger.info("正在提取地名信息...");
        List list = discourseService.mapRemark(text);
        logger.info("提取地名信息完成");
        return list;
    }

    @Override
    public List getAbstract(String text, int count) throws Exception {
        logger.info("正在获取摘要...");
        List list = discourseService.getAbstract(text, count);
        logger.info("获取摘要完成");
        return list;
    }


    @Override
    public Map getTheme(String text) {
        logger.info("正在获取主题信息...");
        Map map = discourseService.getTheme(text);
        logger.info("获取摘要完成");
        return map;
    }

    @Override
    public Map getEmotion(String text) {
        return null;
    }

    @Override
    public List word2Vec(String text) {
        return null;
    }

    @Override
    public Map sensitiveWord(String text, int matchType) {
        logger.info("分析敏感词中...");
        Map map = discourseService.sensitiveWord(text, matchType);
        logger.info("分析敏感词结束...");
        return map;
    }

    @Override
    public Map textNearCalculate(String text_A, String... texts) {
        logger.info("分析相似度词中...");
        Map map = discourseService.textNearCalculate(text_A, texts);
        logger.info("分析相似度结束...");
        return map;
    }

    /**
     * 通过贝叶斯分类器，获取所属类别的概率
     *
     * @param modelTag
     * @param readyClassifyText
     * @return
     */
    @Override
    public Map getBayesClassifer(String modelTag, List readyClassifyText) {
        logger.info("通过贝叶斯模型获取概率中...");
        return bayesService.predict(modelTag, readyClassifyText);
    }

    /**
     * 通过指定格式的训练样本格式，来进行训练贝叶斯模型，并返回模型的Tag
     *
     * @param map
     * @return
     */
    @Override
    public String traniBayesClassfier(Map map) {
        logger.info("训练贝叶斯模型中...");
        String tag = bayesService.trainClassifier("API BAYES", map);
        logger.info("训练贝叶斯模型结束...，tag 是：" + tag);
        return tag;
    }


}
